package org.darcy.framework.blog.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.darcy.framework.blog.file.entity.VirtualFile;
import org.darcy.framework.blog.file.exception.LocalApiException;
import org.darcy.framework.blog.file.util.FileUtil;
import org.darcy.framework.blog.file.util.StreamUtil;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

/**
 * 本地文件上传
 */
public class LocalApiClient extends BaseApiClient {

	private static final String DEFAULT_PREFIX = "oneblog/";

	private String rootPath; // 根路径

	private String pathPrefix;// 子目录

	public LocalApiClient() {
		super("Nginx文件服务器");
	}

	/**
	 * 本地文件上传
	 * 
	 * @param rootPath   上传根路径
	 * @param uploadType 上传类型
	 */
	public LocalApiClient init(String rootPath, String uploadType) {
		this.rootPath = rootPath;
		this.pathPrefix = StringUtils.isEmpty(uploadType) ? DEFAULT_PREFIX
				: uploadType.endsWith("/") ? uploadType : uploadType + "/";
		return this;
	}

	@Override
	public VirtualFile uploadImg(InputStream is, String imageUrl) {
		this.check();

		String key = FileUtil.generateTempFileName(imageUrl);
		this.createNewFileName(key, this.pathPrefix);
		Date startTime = new Date();

		String realFilePath = this.rootPath + this.newFileName;
		FileUtil.checkFilePath(realFilePath);
		try (InputStream uploadIs = StreamUtil.clone(is);
				InputStream fileHashIs = StreamUtil.clone(is);
				FileOutputStream fos = new FileOutputStream(realFilePath)) {
			FileCopyUtils.copy(uploadIs, fos);
			return new VirtualFile().setOriginalFileName(FileUtil.getName(key)).setSuffix(this.suffix)
					.setUploadStartTime(startTime).setUploadEndTime(new Date()).setFilePath(this.newFileName)
					.setFileHash(DigestUtils.md5DigestAsHex(fileHashIs)).setFullFilePath(null);
		} catch (Exception e) {
			throw new LocalApiException("[" + this.storageType + "]文件上传失败：" + e.getMessage() + imageUrl);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public boolean removeFile(String key) {
		this.check();
		if (StringUtils.isEmpty(key)) {
			throw new LocalApiException("[" + this.storageType + "]删除文件失败：文件key为空");
		}
		File file = new File(this.rootPath + key);
		if (!file.exists()) {
			throw new LocalApiException("[" + this.storageType + "]删除文件失败：文件不存在[" + this.rootPath + key + "]");
		}
		try {
			return file.delete();
		} catch (Exception e) {
			throw new LocalApiException("[" + this.storageType + "]删除文件失败：" + e.getMessage());
		}
	}

	@Override
	public void check() {
		if (StringUtils.isEmpty(rootPath)) {
			throw new LocalApiException("[" + this.storageType + "]尚未配置文件服务器，文件上传功能暂时不可用！");
		}
	}
}
